package cn.cau.control;


import cn.cau.entity.NonEnum;
import cn.cau.entity.CodeRule;
import cn.cau.entity.BasicEnum;
import cn.cau.entity.BasicEnumSum;
import cn.cau.service.CodeRuleService;
import cn.cau.service.BasicEnumService;
import cn.cau.service.CodeNumberService;
import cn.cau.service.NonEnumService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;


@Controller
public class CodeGenerateController {

    @Autowired
    private CodeRuleService codeRuleService;
    @Autowired
    private CodeNumberService codeNumberService;
    @Autowired
    private BasicEnumService basicEnumService;
    @Autowired
    private NonEnumService nonEnumService;

    @RequestMapping(value = "/tocodeGenerateInfo")
    public ModelAndView RedirToNext(@RequestParam(value = "id")int id){
        CodeRule c1=codeRuleService.findCodeRuleById(id);
        ModelAndView modelAndView = new ModelAndView();
        int count=c1.getCodeCodeCount();
        List<BasicEnumSum> enumsum=basicEnumService.findBasicEnumSumByCodeRuleId(id);
        List<NonEnum> nonenumsum=nonEnumService.findNonEnumByCodeId(id);
        modelAndView.addObject("count",count);
        modelAndView.addObject("rule",c1);
        modelAndView.addObject("code_count",count);
        modelAndView.addObject("enumsum",enumsum);
        modelAndView.addObject("nonenumsum",nonenumsum);
        modelAndView.setViewName("/codeGenerate/codeGenerateInfo");
        return modelAndView;
    }

    @RequestMapping(value = "/tocodeGenerateResult",method=RequestMethod.POST)
    public ModelAndView RedirToEnd(){
        ModelAndView modelAndView2 = new ModelAndView();
        modelAndView2.setViewName("/codeGenerate/codeGenerateResult");
        return modelAndView2;
    }

    @RequestMapping(value = "/codeGenerate",method=RequestMethod.POST)
    public ModelAndView CodeGenerate(HttpServletRequest request,
                                     HttpSession session,
                                     @RequestParam(value = "categoryOptions")List<String> categoryOptions,
                                     @RequestParam(value = "codeRuleIdd")List<Integer> codeRuleIdd,
                                     @RequestParam(value = "categoryOptionsLength")List<String> categoryOptionsLength
                                     ){
        int coderuleid=codeRuleIdd.get(0);
        CodeRule coderule=codeRuleService.findCodeRuleById((coderuleid));
        int serial=coderule.getSerialNumber();
        int categoryid=coderule.getCategoryId();
        System.out.println(serial);
        int rulecategoryid=coderule.getRuleCategoryId();
        String codenumber=codeNumberService.codeGenerate(serial,coderule, coderuleid, categoryid, rulecategoryid, categoryOptions, categoryOptionsLength);
        ModelAndView modelAndView = new ModelAndView();
        modelAndView.addObject("codenumber",codenumber);
        modelAndView.setViewName("codeGenerate/codeGenerateResult");
        return modelAndView;
    }
}
